﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PicSolve
{
    public static class PicSolver
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="linei"></param>
        /// <returns></returns>
        public static List<PLine> GetSimpleCoverPoss(this PLineInd linei)
        {
            List<PLine> ret = new List<PLine>();
            if (linei.Indices.Length == 0)
            {
                throw new InvalidOperationException("Fournir au moins un indice");
            }

            if (linei.Indices.Length == 1)
            {
                if (linei.Indices[0] == linei.LineLength)
                {
                    ret.Add(new PLine(linei.LineLength, PValue.Box));
                }
                else if (linei.Indices[0] >= linei.LineLength / 2)
                {
                    ret.Add(PLineBuilder.FilledOnLeft(linei.Indices[0], linei.LineLength));
                    ret.Add(PLineBuilder.FilledOnRight(linei.Indices[0], linei.LineLength));

                    //for (int decal = 0; decal <= linei.LineLength - linei.Indices[0]; decal++)
                    //{
                    //    ret.Add(PicHelper.FillInside(decal, linei.Indices[0], linei.LineLength));
                    //}
                }
            }
            else
            {
                int sumInd = linei.Indices.Sum(i => i) + linei.Indices.Length - 1;

                if (sumInd == linei.LineLength)
                {
                    ret.Add(PLineBuilder.FilledWithSimplePattern(linei.Indices, linei.LineLength));
                }
            }

            return ret;
        }

        public static PLineInd TrySolve(this PLineInd pli)
        {
            var lst = pli.GetSimpleCoverPoss();

            if (lst.Count > 0)
            {
                var pl = lst[0];
                for (int i = 1; i < lst.Count; i++)
                {
                    pl = PicHelper.MergeLinePattern(pl, lst[i]);
                }

                pli.Line = pl;
            }
            else
            {
                return pli;
            }

            return pli;
        }
    }
}
